﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

public partial class QuanTri_TaoTaiKhoan : System.Web.UI.Page
{
    SoLienLacDataContext db = new SoLienLacDataContext();
    SoLienLacDienTu cls = new SoLienLacDienTu();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["Username"] == null || Session["Username"].ToString() == "")
        {
            Response.Redirect("~/GiaoDienClient/TrangChu.aspx");
        }
        else
        {
            if (!IsPostBack)
            {
                LoadLenAdmin();
                LoadLengrvGV();
            }
        }
    }
    protected void btnTao_Click(object sender, EventArgs e)
    {
        GridViewRow row = grvAdmin.SelectedRow;
        Label MaAdmin = (Label)row.FindControl("lblMaAdmin");
        Admin ad = db.Admins.SingleOrDefault(p => p.MaAdmin ==int.Parse( MaAdmin.Text));
        txtMatKhau.Attributes.Add("value",txtMatKhau.Text);
        ad.TenDangNhap = txtTenDangNhap.Text;
        ad.TenAdmin = txtAdmin.Text;
        ad.MaAdmin = int.Parse(lblAdmin.Text);
        db.SubmitChanges();
        LoadLenAdmin();
        txtAdmin.Text = "";
        txtMatKhau.Text = "";
        txtTenDangNhap.Text = "";
    }
    protected void btnXoa_Click(object sender, EventArgs e)
    {
        GridViewRow row = grvAdmin.SelectedRow;
        Label MaAdmin = (Label)row.FindControl("lblMaAdmin");
        Admin ad = db.Admins.SingleOrDefault(p => p.MaAdmin == int.Parse(MaAdmin.Text));
        txtMatKhau.Attributes.Add("value",txtMatKhau.Text);
        ad.TenDangNhap = "";
        ad.MatKhau= "";
        db.SubmitChanges();
        LoadLenAdmin();
        txtAdmin.Text = "";
        txtMatKhau.Text = "";
        txtTenDangNhap.Text = "";

    }
    protected void btnMoi_Click(object sender, EventArgs e)
    {
        txtMatKhau.Attributes.Add("value",txtMatKhau.Text);
        txtAdmin.Text = "";
        txtMatKhau.Text = "";
        txtTenDangNhap.Text = "";
    }
    protected void grvAdmin_SelectedIndexChanged(object sender, EventArgs e)
    {
        txtMatKhau.Attributes.Add("value",txtMatKhau.Text);
        GridViewRow row = grvAdmin.SelectedRow;
        Label MaAdmin = (Label)row.FindControl("lblMaAdmin");
        Admin ad = db.Admins.SingleOrDefault(p => p.MaAdmin == int.Parse(MaAdmin.Text));
        txtTenDangNhap.Text = ad.TenDangNhap.ToString();
        txtAdmin.Text = ad.TenAdmin;
        lblAdmin.Text = ad.MaAdmin.ToString();
        txtMatKhau.Text = ad.MatKhau.Trim().ToString();
    }
    void LoadLenAdmin()
    {
        var c = from p in db.Admins select new {p.MaAdmin,p.TenAdmin,p.TenDangNhap};
        grvAdmin.DataSource = c;
        grvAdmin.DataBind();
    }
    protected void grvGV_SelectedIndexChanged(object sender, EventArgs e)
    {
        txtMatKhau.Attributes.Add("value", txtMatKhau.Text);
        GridViewRow row = grvGV.SelectedRow;
        Label MaGV = (Label)row.FindControl("lblMaGV");
        CanBoGiaoVien cb = db.CanBoGiaoViens.SingleOrDefault(p=>p.MaCanBoGV==MaGV.Text);
        txtTenGV.Text = cb.TenCanBo;
        lblMacb.Text = cb.MaCanBoGV;
        txtMatKhau.Text = cb.MatKhau.Trim().ToString();
        
    }
    protected void btnTaoTaiKhoan_Click(object sender, EventArgs e)
    {
        txtMatKhau.Attributes.Add("value", txtMatKhau.Text);
        GridViewRow row = grvGV.SelectedRow;
        Label MaGV = (Label)row.FindControl("lblMaGV");
        CanBoGiaoVien cb = db.CanBoGiaoViens.SingleOrDefault(p => p.MaCanBoGV == MaGV.Text);
        cb.MatKhau = txtMatKhau.Text;
        db.SubmitChanges();
        LoadLengrvGV();
        txtMatKhau.Text = ""; txtTenGV.Text = "";

    }
    protected void btnHuy_Click(object sender, EventArgs e)
    {
        txtMatKhau.Attributes.Add("value", txtMatKhau.Text);
        GridViewRow row = grvGV.SelectedRow;
        Label MaGV = (Label)row.FindControl("lblMaGV");
        CanBoGiaoVien cb = db.CanBoGiaoViens.SingleOrDefault(p => p.MaCanBoGV == MaGV.Text);
        cb.MatKhau = "";
        db.SubmitChanges();
        txtTenGV.Text = "";
        txtMatKhau.Text = "";
        LoadLengrvGV();
    }
    protected void btnLamMoi_Click(object sender, EventArgs e)
    {
        txtTenGV.Text = "";
        txtMatKhau.Attributes.Add("value",txtMatKhau.Text);
        txtMatKhau.Text = "";
    }
    void LoadLengrvGV()
    {
        var c = from p in db.CanBoGiaoViens select new {p.MaCanBoGV,p.TenCanBo };
        grvGV.DataSource = c;
        grvGV.DataBind();
    }
}
